package davelha.common;

import java.rmi.RemoteException;
import davelha.gui.MainGameView;


public class GameControl implements Runnable{
	
	public static final int I_AM_PLAYER_1 = 1;
	public static final int I_AM_PLAYER_2 = 2;
	private BaseGameInterface game = null;
	private MainGameView gameView = null;
	private int whoAmI = -1;
	
	private static final int sleeptime = 100;
	
	public GameControl(BaseGameInterface game, int whoAmI){
		this.game = game;
		this.whoAmI = whoAmI;
	}
	
	public void run(){
		//gameView = new MainGameView(this);
		gameView = new MainGameView(this);
		gameView.setVisible(true);
		
		if(this.whoAmI == GameControl.I_AM_PLAYER_2){
			//inicie-se desabilitado
			//gameView.setEnabled(false);
			gameView.setButtonsEnabled(false);
		}
		
		try{
			
			//O verdadeiro game loop
			while((game.getGameState() == BaseGameInterface.STATE_GAME_START)
					||(game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_ONE_TURN) ||
					(game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_TWO_TURN)){
				
				
				if((game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_ONE_TURN)
						&& (this.whoAmI == GameControl.I_AM_PLAYER_1)){
//					if(!gameView.isEnabled())//TODO Modificar
//						gameView.setEnabled(true);
					if(!gameView.isButtonsEnabled())
						gameView.setButtonsEnabled(true);
					gameView.updateChartData(game.getGameTab()); //Atualiza o quadro atual
				}else if((game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_TWO_TURN)
						&& (this.whoAmI == GameControl.I_AM_PLAYER_2)){
//					if(!gameView.isEnabled())//TODO Modificar
//						gameView.setEnabled(true);		
					if(!gameView.isButtonsEnabled())
						gameView.setButtonsEnabled(true);
					gameView.updateChartData(game.getGameTab()); //Atualiza o quadro atual

				}
				
				try{
					Thread.sleep(GameControl.sleeptime);
				} catch (InterruptedException e){
					e.printStackTrace();
				}
			}
			
			if((game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_ONE_WIN)
					&& (this.whoAmI == GameControl.I_AM_PLAYER_2)){
				//perdeu playboy
				gameView.kickLooser();
				game.setGameState(BaseGameInterface.STATE_GAME_FINISHED);
			}else if((game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_TWO_WIN)
					&& (this.whoAmI == GameControl.I_AM_PLAYER_1)){
				gameView.kickLooser();
				game.setGameState(BaseGameInterface.STATE_GAME_FINISHED);
			}
			
		} catch (RemoteException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param choice
	 * @return
	 */
	public String setChoice(int choice){
		String result = ((this.whoAmI == GameControl.I_AM_PLAYER_1)? "X":"0");
		
		try {
			game.setChar(choice, result.charAt(0));
			
			//Faz a inversao	
			if((whoAmI == GameControl.I_AM_PLAYER_1) && (game.getGameState() ==
				BaseGameInterface.STATE_GAME_PLAYER_ONE_TURN)){
				
				//Player 1 ganhou
				if(game.gameOver()){
					gameView.congratWinner();
					//seta o estado do jogo
					game.setGameState(BaseGameInterface.STATE_GAME_PLAYER_ONE_WIN);
				}else{	
					game.setGameState( 
							BaseGameInterface.STATE_GAME_PLAYER_TWO_TURN);
				}
			}else if((whoAmI == GameControl.I_AM_PLAYER_2) && (
					game.getGameState() == BaseGameInterface.STATE_GAME_PLAYER_TWO_TURN)){
				
				if(game.gameOver()){
					gameView.congratWinner();
					//seta o estado do jogo
					game.setGameState(BaseGameInterface.STATE_GAME_PLAYER_TWO_WIN);
				}else{	
					game.setGameState( 
							BaseGameInterface.STATE_GAME_PLAYER_ONE_TURN);
				}
				
			}
			
			//gameView.setEnabled(false);
			gameView.setButtonsEnabled(false);

		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return (this.whoAmI == 1)? "X":"0";
	}
	
	public String getPlayerName(){
		String name = null;
		try {
			if(whoAmI == GameControl.I_AM_PLAYER_1)
				name = game.getPlayer1().getName();
			else
				name = game.getPlayer2().getName();
				
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		return name;
	}
	
	public String getOponentName(){
		String name = null;
		try {
			if(whoAmI == GameControl.I_AM_PLAYER_1)
				name = game.getPlayer2().getName();
			else
				name = game.getPlayer1().getName();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		return name;
	}
	
	public String getPlayerSignal(){
		if(whoAmI == GameControl.I_AM_PLAYER_1)
			return "X";
		else
			return "0";
	}
	
}

